
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections.Generic;
using System.IO;
using System.Web.UI;

using Swaf.Container;
using Swaf.BizObj;

namespace Swaf.Gui.AspNet2
{
    public class BaseBizObjDisplay : BaseBizObjUserControl, Swaf.Gui.IBizObjDisplay
    {
		protected List<string> m_processedFields = new List<string>();
		protected bool m_changedTemplates = false;

		public BaseBizObjDisplay(): base(){}
		public BaseBizObjDisplay(ActiveWidget widget) : base(widget) { }

		protected string m_templateFromBizObjField = null;

        public virtual void displayBizObj(IBizObj bizObj)
        {
			if (Page == null || Visible==false)return;

			bool needToClear = false;
			//Added this to control how clearControls below is called.  We dont always want to clear controls, even when
			//its not necessary.  This could be breaking stuff in calendar management... need to test there when I can
			//to make sure this didnt break it.
			if (CurrentBizObj != null && bizObj != null && CurrentBizObj.KeyValue != bizObj.KeyValue)
				needToClear = true;
			CurrentBizObj = bizObj;
			string lastContentName = m_contentName;
			string oldContentName = (string)State.State.get("previousTemplateName");
			m_contentName = null;
			string newContentName = ContentName;
			bool differentFromLastRequest = string.Compare(oldContentName, newContentName) != 0;
			bool differentFromLastContent = string.Compare(lastContentName, newContentName) != 0;
			if (differentFromLastRequest || differentFromLastContent)
			{
				//State.State.put("previousTemplateName", newContentName);
				needToClear = true;
				m_changedTemplates = differentFromLastRequest;
				//clearControls();
			}
			//This clearControls was added 5/14/09 because the conditional block could be using something based on the CurrentBizObj
			//If the currentBizObj is changed, it could change what controls are actually created.
			if(needToClear)
				clearControls();

            createControls();
			if(bizObj != null)
				for(int i=0;i<Controls.Count;i++)
					if (processControl(Controls[i]))
						m_processedFields.Add(Controls[i].ID.Replace('_', '.'));
        }

		public bool isDisplayingContent { get { return Visible && !Cmn.IsEmpty(ContentName); } }

        public virtual void updateBizObj(IBizObj bizObj)
        {
			if (Page == null || Visible == false) return;
			CurrentBizObj = bizObj;
			createControls();
			if(bizObj != null)
				foreach (Control ctl in Controls)
					updateBizObjField(ctl);
        }

        public virtual void displayBizObjs(params IBizObj[] objs)
        {
			if (Page == null || Visible == false) return;
//			clearControls();
            createControls();
            foreach (IBizObj obj in objs)
            {
                CurrentBizObj = obj;
                foreach (Control ctl in Controls)
                    processControl(ctl);
            }
        }

        protected override IBizObj getBizObjFromControl(Control ctl, ref string baseCtlId)
        {
			//baseCtlId = ctl.ID.Replace('_', '.');
            return CurrentBizObj;    
        }

        public virtual bool isSelected
        {
            get { throw new Exception("The method or operation is not implemented."); }
        }

        public event BizObjSelectedInfo Selected;

		public string TemplateFromBizObjField
		{
			get { return (string)getOffline("m_templateFromBizObjField"); }
			set { putOffline("m_templateFromBizObjField", value); }
		}

		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);
			if (State != null && State.State.get("m_templateFromBizObjField") == null)
				State.State.put("m_templateFromBizObjField", m_templateFromBizObjField);
		}

		protected override void OnUnload(EventArgs e)
		{
			if (State != null)
				State.State.put("Visible", Visible);
			if(BPC != null && State != null)
                State.State.put("previousTemplateName", ContentName);
			base.OnUnload(e);
		}

		public override bool Visible
		{
			get
			{
				if (State != null && State.State != null)
				{
					object v = State.State.get("Visible");
					return v is bool ? (bool)v : base.Visible;
				}
				return base.Visible;
			}
			set
			{
				State.State.put("Visible", value);
				base.Visible = value;
			}
		}
		public override string ContentName
		{
			get
			{
				if (TemplateFromBizObjField != null && CurrentBizObj != null && EditPrivilege == Swaf.Security.Privileges.ReadOnly)
					return m_contentName = CurrentBizObj.KeyValue.ToString();
				else
					return base.ContentName;
			}
		}

		protected override System.IO.TextReader Html
		{
			get
			{
				if (TemplateFromBizObjField != null && CurrentBizObj != null && EditPrivilege == Swaf.Security.Privileges.ReadOnly)
					return new StringReader(CurrentBizObj.get(TemplateFromBizObjField).ToString());
				return base.Html;
			}
		}
    }
}
